/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import thesis.haipv.qa.model.UserProfile;
import thesis.haipv.qa.persistence.UserProfileDAO;

/**
 *
 * @author PHANHAI
 */
public class Login extends ActionSupport {

    private UserProfile userProfile;
    private Map session;

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public String doLogin() throws Exception {

        UserProfileDAO dao = new UserProfileDAO();
        userProfile = dao.login(userProfile.getUsername(), userProfile.getPassword());

        if (userProfile == null) {
            return "fail";
        } else {
            session = ActionContext.getContext().getSession();

            session.put("user", userProfile);
            System.out.println("Full name: " + userProfile.getFullname());
            return "success";
        }
    }

    public String doLogout() throws Exception {
        session = ActionContext.getContext().getSession();
        session.remove("user");
        return "success";
    }

    public UserProfile getUserProfile() {
        return userProfile;
    }

    public void setUserProfile(UserProfile userProfile) {
        this.userProfile = userProfile;
    }
}
